package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.TextColorBuilder;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ExamineIndexResult;
import cn.dujc.superkid.bean.ExamineListResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;


public class ExaminationActivity  extends BaseActivity {
    public static final String EXTRA_MYTEST = "EXTRA_COURSE";
    private String examineid;
    private TextView tv_content,tv_title_0,tv_info;
    private Button btn_start;
    @Override
    public int getViewId() {
        return R.layout.activity_examination;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        examineid = extras().get(EXTRA_MYTEST);
        setTitle("考试");
        initViews();
        loadData();
    }

    private void initViews() {
        tv_title_0=findViewById(R.id.tv_title_0);
        tv_content=findViewById(R.id.tv_content);
        btn_start=findViewById(R.id.btn_start);
        tv_info=findViewById(R.id.tv_info);

    }


    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getExamineOnline().examineIndex(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("examine_id", examineid)//试卷id
                .create()).enqueue(new ResultCallBack<Result<ExamineIndexResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<ExamineIndexResult> body) {
                final ExamineIndexResult data=body.getData();
                tv_title_0.setText(data.getEx_title());
                tv_content.setText( Html.fromHtml(data.getEx_desc()));
                int color = ContextCompat.getColor(mActivity, R.color.orange);
                tv_info.setText(new TextColorBuilder()
                        .addTextPart("共计")
                        .addTextPart(color,data.getQuestions_number())
                        .addTextPart("题，考试时间")
                        .addTextPart(color,data.getEx_usedtime())
                        .addTextPart("分钟")
                        .build());
                btn_start.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        starter()
                                .with(ExamAnswerActivity.EXTRA_MYTEST,examineid)
                                .go(ExamAnswerActivity.class,true);
                    }
                });
            }
        });
    }
}
